import { Init, Inject, Provide } from '@midwayjs/core';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { FixturesMouldEntity } from '../entity/mould';
import { PluginService } from '../../plugin/service/info';

/**
 * 页面
 */
@Provide()
export class FixturesMouldService extends BaseService {
  @InjectEntityModel(FixturesMouldEntity)
  fixturesMouldEntity: Repository<FixturesMouldEntity>;
  @Inject()
  pluginService: PluginService;
  @Init()
  async init() {
    await super.init();
    this.setEntity(this.fixturesMouldEntity);
  }

  /**
   * 更新页面
   */
  async update(param: FixturesMouldEntity) {
    if (param.isHome) {
      await this.fixturesMouldEntity.update({ isHome: 1 }, { isHome: 0 });
    }
    await this.fixturesMouldEntity.save(param);
  }

  /**
   * 获取首页
   */
  async getHomePage() {
    const data = await this.fixturesMouldEntity.findOne({
      where: { isHome: 1 },
    });
    if (!data || !data.status) throw new CoolCommException('该页面已丢失~');
    return data;
  }

  /**
   * 获取指定页面
   * @param id
   */
  async getPage(id: string) {
    if (!Number(id)) throw new CoolCommException('该页面已丢失~');
    const data = await this.fixturesMouldEntity.findOne({
      where: { id: Number(id) },
    });
    if (!data || !data.status) throw new CoolCommException('该页面已丢失~');

    if (!data.data) throw new CoolCommException('该页面未发布~');

    return data;
  }

  /**
   * 获取页面预览小程序码
   * 需要调用 wx 插件 https://www.cool-admin.com/plugin/70
   *因为不注释掉，连 npm run dev 都过不了。。。
   */
  async getFixturesPreviewCode(body: any) {
    throw new CoolCommException(
      '请手动修改关闭代码注释 在 fixtures-service-mould-getFixturesPreviewCode'
    );
    // const { id, form } = body
    // const info = await this.fixturesMouldEntity.findOne({ where: { id: id } })
    // if (!info) throw new CoolCommException('数据不存在~');
    // await this.fixturesMouldEntity.update({ id: info.id }, { form: form })
    // // 获取插件实例
    // const instance = await this.pluginService.getInstance('wx');
    // // 获得小程序实例
    // const MiniApp = await instance.MiniApp();
    // const miniToken = await MiniApp.getAccessToken().getToken();
    // let miniUrl = `https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=${miniToken}`
    // // 获取 api 调用客户端
    // let client = MiniApp.getClient();
    // const data = {
    //     page: "uni_modules/cool-fixtures/pages/preview",
    //     scene: `id=${info.id}`,
    //     width: 200,
    //     check_path: false,
    // }
    // const result = await client.post(miniUrl, { data, responseType: 'arraybuffer' });
    // const buffer = Buffer.from(result.response.data, 'base64');
    // return 'data:image/png;base64,' + buffer.toString('base64');
  }

  /**
   * 获取平台页面
   */
  async getPlatformPages() {
    const data = [
      {
        name: '基础页面',
        children: [
          { name: '平台首页', page: '/pages/index/home' },
          { name: '轻松赚钱', page: '/pages/index/introduce' },
          { name: '购物车', page: '/pages/index/shopping-cart' },
          { name: '商品分类', page: '/pages/index/category' },
          { name: '会员中心', page: '/pages/index/my' },
          { name: '订单列表', page: '/pages/order/list' },
          { name: '我的收益', page: '/pages/user/balance/purse' },
          { name: '在线客服', page: '/uni_modules/cool-cs/pages/chat' },
          { name: '附近终端店', page: '/pages/partnership/sample/vicinity' },
        ],
      },
      {
        name: '我的店铺',
        children: [
          { name: '店铺首页', page: '/pages/partnership/index' },
          { name: '店铺订单', page: '/pages/partnership/order' },
          { name: '店铺码', page: '/pages/partnership/code' },
          { name: '店铺代金券', page: '/pages/partnership/voucher/index' },
          { name: '店铺拉客工具', page: '/pages/partnership/activity/index' },
        ],
      },
      {
        name: '抽奖活动',
        children: [
          { name: '期数集奖', page: '/pages/activity/index' },
          {
            name: '九宫格抽奖',
            page: '/pages/partnership/activity/lottery/detail',
          },
          {
            name: '邀请赢红包',
            page: '/pages/partnership/activity/laxin/detail',
          },
          {
            name: '邀请赢代金券',
            page: '/pages/partnership/activity/lake/detail',
          },
        ],
      },
    ];

    return data;
  }
}
